//
//  AnswerQuestion.cpp
//  QuizMaster
//
//  Created by Jeppe Nielsen on 3/23/15.
//  Copyright (c) 2015 Jeppe Nielsen. All rights reserved.
//

#include "AnswerQuestion.hpp"
#include "GameWorld.hpp"
#include "MathHelper.hpp"
#include <sstream>
#include "SoundSystem.hpp"

using namespace Nano;

void AnswerQuestion::Initialize() {
    gui = Parent().gui;
    
    
    
    Vector2 size(10.0f,1.0f);
    
    moveOut.position.Add(0, 0);
    moveOut.position.Add(0.3f, {0,1.5f,0});
    
    moveIn.position.Add(0, {0,1.5f,0});
    moveIn.position.Add(0.3f, 0);
    
    currentPlayerSign = gui->CreateControl(0, "Background", Vector3(0,3,0), size);
    currentPlayerSign->GetComponent<Transform>()->Anchor = size*0.5f;
    currentPlayerSign->AddComponent<TransformAnimator>()->SetFromLastFrame(&moveOut);
    
    playerName = gui->CreateLabel(currentPlayerSign, 0, size, Parent().font, "", 0.6f);
    playerName->GetComponent<Label>()->HAlignment = Font::Center;
    playerName->GetComponent<Label>()->VAlignment = Font::Middle;
    
    Vector2 buttonSize(2,2);
    
    correctButton = gui->CreateControl(0, "Background", Vector3(-2,10,0), buttonSize);
    correctButton->GetComponent<Transform>()->Anchor = buttonSize*0.5f;
    correctButton->GetComponent<Colorable>()->Color = Colour::Green();
    correctButton->GetComponent<Material>()->Mask = 1;
    
    
    wrongButton = gui->CreateControl(0, "Background", Vector3(2,10,0), buttonSize);
    wrongButton->GetComponent<Transform>()->Anchor = buttonSize*0.5f;
    wrongButton->GetComponent<Colorable>()->Color = Colour::Red();
    wrongButton->GetComponent<Material>()->Mask = 1;
    
    
    barMoveOut.position.Add(0, 0);
    barMoveOut.position.Add(0.3f, {0,-1,0});
    
    barMoveOut.scale.Add(0, 0);
    
    barMoveIn.position.Add(0, {0,-1,0});
    barMoveIn.position.Add(0.3f, 0);
    
    Vector2 barSize(10.0f,0.5f);
    
    bar = gui->CreateControl(0, "Background", Vector3(0,-3,0), barSize);
    bar->GetComponent<Transform>()->Anchor = barSize*0.5f;
    bar->AddComponent<TransformAnimator>()->SetFromLastFrame(&barMoveOut);
    bar->GetComponent<Colorable>()->Color = Colour::Blue();

    barAnimation.scale.Add(0, 1.0f);
    barAnimation.scale.Add(4.0f, {0,1.0f,1.0f});
    
    questionMoveout.rotation.Add(0, {0,ToRadians(180),0});
    questionMoveout.rotation.Add(0.5f, {ToRadians(90),ToRadians(180),0});
    
    correctButton->GetComponent<Touchable>()->Click += event_handler(this, &AnswerQuestion::CorrectClicked);
    wrongButton->GetComponent<Touchable>()->Click += event_handler(this, &AnswerQuestion::WrongClicked);
    
    size = Vector2(8,0.7f);
    
    answerFrame = gui->CreateControl(0, "Background", Vector3(0,-3,0), size);
    answerFrame->GetComponent<Transform>()->Anchor = size*0.5f;
    answerFrame->GetComponent<Transform>()->Position = Vector3(0,-10,0);
    answerFrame->GetComponent<Material>()->Mask = 1;
    answerFrame->GetComponent<Colorable>()->Color = Colour::Black();
    
    answerLabel = gui->CreateLabel(answerFrame, 0, size, Parent().font, "Jeppe", 0.5f);
    answerLabel->GetComponent<Label>()->HAlignment = Font::Center;
    answerLabel->GetComponent<Label>()->VAlignment = Font::Middle;
    answerLabel->GetComponent<Material>()->Mask = 1;
    
    
    
    correctSound = Parent().world.CreateObject();
    correctSound->AddComponent<Sound>()->LoadFromWav("correct.wav");
    correctSound->AddComponent<SoundEmitter>()->Looping = false;
    correctSound->GetComponent<SoundEmitter>()->Playing = false;
    
    wrongSound = Parent().world.CreateObject();
    wrongSound->AddComponent<Sound>()->LoadFromWav("wrong.wav");
    wrongSound->AddComponent<SoundEmitter>()->Looping = false;
    wrongSound->GetComponent<SoundEmitter>()->Playing = false;
    
    
    timerSound = Parent().world.CreateObject();
    timerSound->AddComponent<Sound>()->LoadFromWav("timer.wav");
    timerSound->AddComponent<SoundEmitter>()->Looping = true;
    timerSound->GetComponent<SoundEmitter>()->Playing = false;
    
    timesUpSound = Parent().world.CreateObject();
    timesUpSound->AddComponent<Sound>()->LoadFromWav("timesUp.wav");
    timesUpSound->AddComponent<SoundEmitter>()->Looping = false;
    timesUpSound->GetComponent<SoundEmitter>()->Playing = false;
    
}

void AnswerQuestion::Update(float dt) {
    if (timer>0) {
        timer -= dt;
        if (!(timer>0)) {
            timerSound->GetComponent<SoundEmitter>()->Playing = false;
            timesUpSound->GetComponent<SoundEmitter>()->Emit();
        }
    }
}

void AnswerQuestion::End(bool correctAnswer) {
    GameObject* currentQuestion = Parent().data.CurrentQuestion;
    
    int points = currentQuestion->GetComponent<QuestionComponent>()->question->points;
    
    if (!Parent().data.PlayerToAnswerQuestion()) {
        correctAnswer = true;
    }
    
    if (correctAnswer) {
    
        if (Parent().data.PlayerToAnswerQuestion()) {
            Parent().data.CurrentPlayer = Parent().data.PlayerToAnswerQuestion;
            Parent().data.CurrentPlayer()->GivePoints(points);
            correctSound->GetComponent<SoundEmitter>()->Emit();
        }
        
        currentQuestion->Remove();
        Parent().data.CurrentQuestion = 0;
        Parent().CurrentState = "SelectQuestion";
    } else {
        Parent().data.PlayerToAnswerQuestion()->GivePoints(-points);
        Parent().data.PlayerToAnswerQuestion()->BuzzerEnabled = false;
        Parent().CurrentState = "WaitForBuzz";
        
        wrongSound->GetComponent<SoundEmitter>()->Emit();
    }
}

void AnswerQuestion::Render() {

}

void AnswerQuestion::CorrectClicked(TouchData e) {
    End(true);
}

void AnswerQuestion::WrongClicked(TouchData e) {
    End(false);
}

void AnswerQuestion::Enabled() {

    if (Parent().data.PlayerToAnswerQuestion()) {

    std::stringstream s;
    s<<Parent().data.PlayerToAnswerQuestion()->Name;
    playerName->GetComponent<Label>()->Text = s.str();
    currentPlayerSign->ToFront();
    currentPlayerSign->GetComponent<TransformAnimator>()->Play(&moveIn);
    bar->GetComponent<TransformAnimator>()->Play(&barMoveIn);
    bar->GetComponent<TransformAnimator>()->Play(&barAnimation, 0.3f);
    bar->ToFront();
    bar->GetComponent<Colorable>()->Color = Parent().data.PlayerToAnswerQuestion()->color;
    currentPlayerSign->GetComponent<Colorable>()->Color = bar->GetComponent<Colorable>()->Color;
    timer = 4;
    
    timerSound->GetComponent<SoundEmitter>()->Playing = true;
    
    }
    
    GameObject* question = Parent().data.CurrentQuestion();
    /*
    question->GetComponent<Transform>()->Matrix = Matrix4x4::IDENTITY;
    questionMoveout.position.nodes.Clear();
    questionMoveout.position.Add(0.0f, question->GetComponent<Transform>()->WorldInverse.GetValue()->TransformPosition(0));
    
    questionMoveout.scale.nodes.Clear();
    questionMoveout.scale.Add(0.0f, 3.5f);
    questionMoveout.scale.Add(0.5f, 0.0f);
    
    questionMoveout.rotation.nodes.Clear();
    questionMoveout.rotation.Add(0, {0,ToRadians(180),0});
    //questionMoveout.rotation.Add(0.5f, {ToRadians(0),ToRadians(0),0});
    questionMoveout.positionPlayMode = TransformAnimationPlayMode::Clamp;
    
    question->GetComponent<TransformAnimator>()->Play(&questionMoveout);
    */
    correctButton->GetComponent<Transform>()->Position = {2,-2,0};
    correctButton->ToFront();
    
    wrongButton->GetComponent<Transform>()->Position = {-2,-2,0};
    wrongButton->ToFront();
    
    
    answerFrame->ToFront();
    
    answerLabel->GetComponent<Label>()->Text = question->GetComponent<QuestionComponent>()->question->answer;
    
    answerFrame->GetComponent<Transform>()->Position = Vector3(0,(Parent().data.PlayerToAnswerQuestion()) ? 2 : 0,0);
    answerFrame->GetComponent<Material>()->Mask =  (Parent().data.PlayerToAnswerQuestion()) ? 1 : 0;
    answerLabel->GetComponent<Material>()->Mask = answerFrame->GetComponent<Material>()->Mask;
    
    bar->ToFront();
}

void AnswerQuestion::Disabled() {
    currentPlayerSign->GetComponent<TransformAnimator>()->Play(&moveOut);
    bar->GetComponent<TransformAnimator>()->Play(&barMoveOut,0.3f);
   
    correctButton->GetComponent<Transform>()->Position = {2,10,0};
    wrongButton->GetComponent<Transform>()->Position = {-2,10,0};
    
    answerFrame->GetComponent<Transform>()->Position = Vector3(0,-10,0);
    
    timerSound->GetComponent<SoundEmitter>()->Playing = false;
}

